#pragma once
#include <iostream>
#include <unordered_map>
#include <vector>
#include <assert.h>
#include <fstream>
#include "../comm/Log.hpp"
#include "../comm/Util.hpp"
namespace ns_model
{
    using namespace ns_log;
    using namespace ns_util;
    struct Question
    {
        std::string number; // 题目编号
        std::string title;  // 题目标题
        std::string star;   // 题目难度：简单 中等 困难
        int cpu_limit;      // 限制程序运行时间(s)
        int mem_limit;      // 限制程序占用内存(KB)

        std::string desc;   // 题目描述
        std::string header; // 默认代码
        std::string tail;   // 测试代码

        // 其中header tail 两个代码需要拼接构成完整代码
    };

    const std::string question_list = "./questions/questions.list";
    const std::string question_path = "./questions/";
    class Model
    {

    public:
        Model()
        {
            assert(LoadQuestionList(question_list));
        }
        ~Model()
        {
        }
        // 加载题目配置文件
        bool LoadQuestionList(std::string question_list /*题目配置文件目录*/)
        {
            std::ifstream in(question_list);
            if (!in.is_open())
            {
                LOG(FATAL) << "打开配置文件失败.." << "\n";
                return false;
            }
            std::string line;
            while (getline(in, line))
            {
                std::vector<std::string> tokens;
                StringUtil::SplitString(line, &tokens, " ");
                if (tokens.size() != 5)
                {
                    // 配置文件有问题
                    LOG(WARNING) << "配置文件存在问题，某题目未加载" << "\n";
                    continue;
                }
                Question q;
                q.number = tokens[0];
                q.title = tokens[1];
                q.star = tokens[2];
                q.cpu_limit = atoi(tokens[3].c_str());
                q.mem_limit = atoi(tokens[4].c_str());
                std::string path = question_path + tokens[0] + '/';

                FileUtil::ReadFile(path + "desc.txt", &q.desc, true);
                FileUtil::ReadFile(path + "header.cpp", &q.header, true);
                FileUtil::ReadFile(path + "tail.cpp", &q.tail, true);
                questions.insert({q.number, q});
            }
            in.close();
            LOG(INFO) << "题目加载成功" << "\n";
            return true;
        }
        bool GetAllQuestion(std::vector<Question>* out)
        {
            if(questions.size()==0)
            {
                LOG(ERROR)<<"用户获取题库失败"<<"\n";
                return false;
            }
            for(auto& q:questions)
            {
                out->push_back(q.second);
            }
            LOG(INFO)<<"用户获取题库成功"<<"\n";
            return true;
        }
        bool GetOneQuestion(const std::string number,Question* question)
        {
            auto iter=questions.find(number);
            if(iter==questions.end())
            {
                LOG(ERROR)<<"用户获取题目失败，题目编号:"<<number<<"\n";
                return false;
            }
            *question=iter->second;
            LOG(INFO)<<"用户获取题目成功，题目编号:"<<number<<"\n";
            return true;
        }
    private:
        std::unordered_map<std::string, Question> questions; // 题目编号与题目属性＋题目映射
    };
}